package br.com.bafonline.controller.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.bafonline.util.Crypter;
import br.com.bafonline.util.Util;
import br.com.bafonline.util.struts.action.BaseAction;

/**
 * Action (View) que prepara a tela de login, gerando um CAPTCHA.
 * @author bafonline
 *
 */
public class PrepareLoginAction extends BaseAction {
	static Logger logger = Logger.getLogger(PrepareLoginAction.class);

	@Override
	public ActionForward processExecute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		logger.debug("*** Executando PrepareLoginAction.processExecute(...) ***");

		String captcha = Util.randomString(5);
		request.setAttribute("captcha", captcha);
		request.setAttribute("secret", Crypter.encrypt(captcha));

		if (request.getParameter("captcha") != null &&
			"invalid".equals(request.getParameter("captcha"))) {
			request.setAttribute("captcha_error", new Boolean(true));
		}

		if (request.getParameter("login") != null &&
			"invalid".equals(request.getParameter("login"))) {
			request.setAttribute("login_error", new Boolean(true));
		}


		return mapping.findForward(SUCCESS);
	}

}
